<?php

namespace App\HttpController;

use App\Service\QuestionnaireService;
use App\Utility\Verification;
use EasySwoole\HttpAnnotation\AnnotationTag\Api;
use EasySwoole\HttpAnnotation\AnnotationTag\ApiDescription;
use EasySwoole\HttpAnnotation\AnnotationTag\ApiGroup;
use EasySwoole\HttpAnnotation\AnnotationTag\Method;
use EasySwoole\HttpAnnotation\AnnotationTag\Param;
use EasySwoole\Mysqli\Exception\Exception;
use Throwable;

/**
 * @ApiGroup(groupName="调查问卷")
 */
class Questionnaire extends BaseController
{
    /**
     * @Api(name="调查问卷开关", path="/questionnaire/getQuestionStatus", version="1.0.0")
     * @ApiDescription(value="调查问卷开关")
     * @Method(allow={POST})
     * @return bool
     * @throws Exception
     * @throws \EasySwoole\ORM\Exception\Exception
     * @throws Throwable
     */
    public function getQuestionStatus(): bool
    {
        $res = QuestionnaireService::getInstance()->getQuestionStatus();
        if (!$res['result']) {
            return $this->writeJsonError($res['msg']);
        }
        return $this->writeJsonSuccess($res['data'], $res['msg']);
    }

    /**
     * @Api(name="调查问卷表单", path="/questionnaire/getQuestion", version="1.0.0")
     * @ApiDescription(value="调查问卷表单")
     * @Method(allow={POST})
     * @Param(name="id", description="id", from={JSON})
     * @return bool
     * @throws Throwable
     * @throws \EasySwoole\ORM\Exception\Exception
     */
    public function getQuestion($id): bool
    {
        $res = QuestionnaireService::getInstance()->getQuestion($id);
        if (!$res['result']) {
            return $this->writeJsonError($res['msg']);
        }

        return $this->writeJsonSuccess($res['data'], $res['msg']);
    }

    /**
     * @Api(name="提交调查问卷", path="/questionnaire/setQuestion", version="1.0.0")
     * @ApiDescription(value="提交调查问卷")
     * @Method(allow={POST})
     * @Param(name="id", description="id", from={JSON})
     * @Param(name="name", description="姓名", required="姓名必填", from={JSON})
     * @Param(name="tel", description="电话", required="手机号必填", from={JSON})
     * @Param(name="house", description="房屋", required="房屋号必填", from={JSON})
     * @Param(name="content", description="内容", from={JSON})
     * @return bool
     * @throws Throwable
     * @throws \EasySwoole\ORM\Exception\Exception
     */
    public function setQuestion($id, $name, $tel, $house, $content): bool
    {
        $checkPhone = Verification::getInstance()->is_phone($tel);
        if (!$checkPhone){
            return $this->writeJsonError('手机号输入错误');
        }

        $res = QuestionnaireService::getInstance()->setQuestion($id, $name, $tel, $house, $content);

        if (!$res['result']) {
            return $this->writeJsonError($res['msg']);
        }

        return $this->writeJsonSuccess($res['data'], $res['msg']);
    }
}